﻿@{
    ViewBag.Title = ViewBag.ParkingAreaName + "停车区立柱列表";
    Layout = "~/Views/Shared/_Layout.cshtml";
    <script type="text/javascript">
        function DeleteLocation(locationId) {
            if (confirm("您确定要删除吗？")) {
                $.ajax({
                    url: "/parkingAreas/DeleteLocation/" + locationId,
                    cache: false,
                    success: function(res) {
                        if (res) {
                            $("#tr" + locationId).remove();
                            alert("删除成功。");
                        } else {
                            alert("删除失败。");
                        }
                    }
                });
            }
        }
        function Recover(locationId, el) {
            var t = $(el).html();
            $.ajax({
                url: "/ParkingAreas/RecoverScanCode/" + locationId,
                cache: false,
                success: function (res) {
                    if (res) {
                        if (t == "回收") {
                            $("#td" + locationId).html("");
                            $(el).html("绑定");
                        } else {
                            $("#td" + locationId).html("<a href=\"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + res + "\" target=\"_blank\">" + res + "</a>");
                            $(el).html("回收");
                        }
                        alert("修改成功。");
                    } else {
                        alert("修改失败。");
                    }
                }
            });
        }
        function GetIds(isBind) {
            var ids = "";
            $("#tBodyRows").find("input[type='checkbox']:checked").each(function(index, el) {
                var id = el.value;
                if(isBind&&$.trim($("#td"+id).html()).length==0)
                    ids += id + ",";
                else if($.trim($("#td"+id).html()).length>0 &&!isBind)
                    ids+=id + ",";
            });
            if(ids.length>0)
                return ids.substr(0, ids.length - 1);
            return ids;
        }
        $(document).ready(function() {

            $("#btnSetCache").click(function() {
                $.ajax({
                    url: "/parkingAreas/UpdateCacheLocations",
                    cache: false,
                    success: function(res) {
                        alert("缓存数据节点成功。");
                    }
                });
            });

            $("#btnBind").click(function() {
                $.ajax({
                    url:"/parkingAreas/BindScanCode",
                    data: {
                        ids: GetIds(true),
                        t:"bind"
                    },
                    cache:false,
                    success:function(res) {
                        if (res.length>0) {
                            var i = 0;
                            var arr = res.toString().split(',');
                            $("#tBodyRows").find("input[type='checkbox']:checked").each(function(index, el) {
                                if($.trim($("#td"+el.value).html()).length==0)
                                {
                                    var locationId = $(el).val();
                                    $("#td" + locationId).html("<a href=\"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + arr[i] + "\" target=\"_blank\">" + arr[i] + "</a>");
                                    $("#a"+locationId).html("回收");
                                    i++;
                                }
                            });
                            alert("绑定成功！");
                        }
                        else
                            alert("绑定失败！");
                    }
                })
            });
            $("#btnCancel").click(function() {
                $.ajax({
                    url: "/parkingAreas/BindScanCode",
                    data: {
                        ids: GetIds(false),
                        t: "cancel"
                    },
                    cache: false,
                    success: function(res) {
                        if (res.length > 0) {
                            $("#tBodyRows").find("input[type='checkbox']:checked").each(function(index, el) {
                                if($.trim($("#td"+el.value).html()).length>0)
                                {
                                    var locationId = $(el).val();
                                    $("#td" + locationId).html("");
                                    $("#a" + locationId).html("绑定");
                                }
                            });
                            alert("回收成功！");
                        } else
                            alert("回收失败！");
                    }
                });
            });
            $("#cbx_selectAll").click(function() {
                $("#tBodyRows").find("input[type='checkbox']").prop("checked", $(this).prop("checked"));
            });
        });
    </script>
}
@using AFT.Parking.Entity.Parkings;
@using AFT.Parking.Foundation;
@using AFT.Parking.WebSite.Common

@model IPagedList<LocationEx>
<div class="content-box">
    <!-- Start Content Box -->
    <div class="content-box-header">
        <h3>@ViewBag.ParkingAreaName 停车区立柱列表</h3>

        <div class="clear"></div>
    </div>
    <form id="frm">
        <!-- End .content-box-header -->
        <div class="content-box-content">
            <div class="tab-content default-tab" id="tab1">
                @Html.Hidden("parkingAreaId")
                <!-- This is the target div. id must match the href of this div's tab -->
                <div class="notification attention png_bg">
                    空闲二维码：@ViewBag.ScanCodeCount  个&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   名称：@Html.TextBox("name", null, new { @class = "searchBox" })
                    状态：@Html.DropDownList("bindId")
                    <input type="submit" class="button" id="btnSubmit" value="查询" />
                    <input type="button" class="button" id="btnBind" value="绑定选择" />
                    <input type="button" class="button" id="btnCancel" value="回收选择" />
                    <input type="button" class="button" id="btnSetCache" value="缓存节点数据" />
                    @{
                        int parkingAreaId = Convert.ToInt32(ViewBag.ParkingAreaId);
                    }
                    @if (parkingAreaId > 0)
                    {
                        <a href="/parkingAreas/addLocation/@parkingAreaId">新增停车区立柱</a>
                    }
                </div>
                <table>
                    <thead>
                        <tr>
                            <th><input type="checkbox" id="cbx_selectAll" /></th>
                            <th>名称</th>
                            <th>状态</th>
                            <th>岔口</th>
                            <th>坐标（x-y）</th>
                            <th>打印个数</th>
                            <th>二维码票据</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tfoot>
                        <tr>
                            <td colspan="6">
                                <div class="pagination">
                                    @Html.Pager(Model)
                                </div>
                                <!-- End .pagination -->
                                <div class="clear"></div>
                            </td>
                        </tr>
                    </tfoot>
                    <tbody id="tBodyRows">
                        @foreach (var item in Model.Rows)
                        {
                            <tr id="@("tr"+item.ParkingLocation.Id)">
                                <td>
                                    <input type="checkbox" name="cbx_select" value="@item.ParkingLocation.Id" />
                                </td>
                                <td>
                                    @item.ParkingLocation.Name
                                </td>
                                <td>
                                    @(item.ParkingLocation.IsEnable ? "正常" : "停用")
                                </td>
                                <td>
                                    @(item.ParkingLocation.IsFork ? "是" : "否")
                                </td>
                                <td>
                                    @item.ParkingLocation.LocationX - @item.ParkingLocation.LocationY
                                </td>
                                <td>
                                    @item.ParkingLocation.PrintScanCodeCount
                                </td>
                                <td id="@("td" + item.ParkingLocation.Id)">
                                    @{bool isBind = false;}
                                    @if (item.ScanCode != null)
                                    {
                                        isBind = true;
                                        <a href="/parkingAreas/PrintScanCode/@item.ParkingLocation.Id" target="_blank">@item.ScanCode.Ticket</a>
                                    }
                                </td>
                                <td>
                                    <a href="/parkingAreas/editeLocation/@item.ParkingLocation.Id" title="Edit">编辑</a>
                                    <a id="@("a" + item.ParkingLocation.Id)" href="javascript:void(0)" onclick="Recover(@item.ParkingLocation.Id,this)" title="Edit">@(isBind ? "回收" : "绑定")</a>
                                    <a href="javascript:void(0);" title="Edit" onclick="DeleteLocation(@item.ParkingLocation.Id)">删除</a>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
            <!-- End #tab1 -->
        </div>
        <!-- End .content-box-content -->
    </form>
</div>
